<?php
// +-------------------------------------------------------------+
// | Tinysupe [ SIMPLE IS THE BEST ! ]
// +-------------------------------------------------------------+
// | Copyright (c) 2011 www.srccn.com All rights reserved.
// +-------------------------------------------------------------+
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +-------------------------------------------------------------+
// | Author: tinysu <tinysu@qq.com>
// +-------------------------------------------------------------+

class dbase
{
	private $driver;
	public $dburl, $charset, $timezone;

	function __construct($dburl=null) {
		defined('COREDIR') or die('Loader error!');
		
		$this->driver = null;
		$this->charset = 'utf8';
		$this->timezone = '+8:00';
		if (!is_null($dburl)) $this->open($dburl);
	}
	
	public function open($dburl='') {
		if (is_null($this->driver)) {
			if ($dburl!='') $this->dburl = $dburl;
			$dburl = parse_url($this->dburl);
			$dfile = COREDIR.'/drivers/'.$dburl['scheme'].'.php';
			if (file_exists($dfile)) {
				require($dfile);
				if (class_exists($dburl['scheme'])) {
					$this->driver = new $dburl['scheme']();
					return $this->driver->open($this->dburl);
				} elseif (class_exists('drv_'.$dburl['scheme'])) {
					$dburl['scheme'] = 'drv_'.$dburl['scheme'];
					$this->driver = new $dburl['scheme']();
					return $this->driver->open($this->dburl);
				}
			}
		}
		return false;
	}

	public function query($sql){
		if ($this->driver) {
			return $this->driver->query($sql);
		}
		return false;
	}

	public function query_one($sql){
		if ($this->driver) {
			return $this->driver->query_one($sql);
		}
		return false;
	}
	
	public function query_row($sql){
		if ($this->driver) {
			return $this->driver->query_row($sql);
		}
		return false;
	}
	
	public function query_all($sql){
		if ($this->driver) {
			return $this->driver->query_all($sql);
		}
		return false;
	}
	
	public function query_page($sql, $pnum=1, $psiz=20){
		if ($this->driver) {
			return $this->driver->query_page($sql, $pnum=1, $psiz);
		}
		return false;
	}

	public function check(){
		if ($this->driver) {
			return $this->driver->check();
		}
		return false;
	}

	public function escape($str){
		if ($this->driver) {
			return $this->driver->escape($str);
		}
		return false;
	}
	
	public function close() {
		if ($this->driver) $this->driver->close();
	}
	
	function __destruct(){
		if($this->driver) $this->close();
	}
}
?>